package model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import physics.*;

public class LeftFlipper implements IGizmo{
	
	private String name;
	private int L;
	private double x;
	private double y;
	private Color col;
	private LineSegment[] lines;
	private Circle[] ends;
	private Vect reflection = null;
	private int rotation = 0;
	private int rotateMode = 0;
	private AffineTransform transform = new AffineTransform();
			
	public LeftFlipper(int gridL) {
		L = gridL;
		col = new Color(0xFFFF00);
	}
	
	public LeftFlipper(int gridL, String s) {
		name = s;
		L = gridL;
		col = new Color(0xFFFF00);
	}
	
	
	@Override
	public void setPosition(int xpos, int ypos) {
		x = xpos * L; //top left corner in pixels
		y = ypos * L; //top left corner in pixels
		
		double r = L/4; // radius of end caps
		
		ends = new Circle[2];
		ends[0] = new Circle(x+r, y+r, r);
		ends[1] = new Circle(x+r, y+7*r, r);
		
		lines = new LineSegment[2];
		lines[0] = new LineSegment(x, y+r, x, y+6*r);
		lines[1] = new LineSegment(x+2*r, y+r, x+2*r, y+6*r);
	}
	
	@Override
	public Graphics paintShape(Graphics g) {
		isRotating();
		double d = L/2;
		
		Area shape = new Area(new RoundRectangle2D.Double(x, y, d, d*4, d, d));
		transform.setToRotation(Math.toRadians(rotation), x, y);
		shape.transform(transform);
		
		Graphics2D g2d = (Graphics2D)g;
		g2d.setColor(col);	
		g2d.fill(shape);
		return g;
	}
	
	private void isRotating() {
		if (rotateMode == 1) {
			if (rotation <= 0 && rotation > -90) {
				rotation -= 15;
				System.out.println(rotation);
				Vect centre = new Vect(x, y);
				for (int i=0 ; i < lines.length ; i++)
					lines[i] = Geometry.rotateAround(lines[i], centre, new Angle(Math.toRadians(-15)));
				for (int i=0 ; i < ends.length ; i++)
					ends[i] = Geometry.rotateAround(ends[i], centre, new Angle(Math.toRadians(-15)));
			}
			else 
				rotateMode = 0;
		}
		if (rotateMode == -1) {
			if (rotation >= -90 && rotation <= -10) {
				rotation += 15;
				Vect centre = new Vect(x, y);
				for (int i=0 ; i < lines.length ; i++)
					lines[i] = Geometry.rotateAround(lines[i], centre, new Angle(Math.toRadians(15)));
				for (int i=0 ; i < ends.length ; i++)
					ends[i] = Geometry.rotateAround(ends[i], centre, new Angle(Math.toRadians(15)));
			}
			else 
				rotateMode = 0;
		}
	}
	

	@Override
	public void addLinkedGizmo(IGizmo giz) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addLinkedKey(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doLinkedAction() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Rectangle getBounds() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Color getColor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<IGizmo> getLinkedGizmos() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Integer> getLinkedKeys() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "LeftFlipper";
	}

	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Vect reflectionVector(Ball ball) {
		return reflection;
	}

	@Override
	public void rotateClockwise() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setColor(Color col) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double timeUntilCollision(Ball ball) {
		double time=Double.POSITIVE_INFINITY, temp=0;

		for (LineSegment line : lines) {
			temp = Geometry.timeUntilWallCollision(line, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectWall(line, ball.getV()); 
			}
		}

		for (Circle end : ends) {
			temp = Geometry.timeUntilCircleCollision(end, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectCircle(end.getCenter(), ball.getCircle().getCenter(), ball.getV());
			}
		}
		return time;
	}

	@Override
	public void triggerEvent() {
		if (rotateMode == 0 ) { // flipper is still
			if (rotation == 0)
				rotateMode = 1;
			if (rotation == -90)
				rotateMode = -1;
		}
		else // flipper is currently moving
			rotateMode = -rotateMode;	
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public int getTriangleRotations() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
}
